<!-- subpkg_archive/list/index.vue -->
<script setup>
  import { ref } from 'vue'
  import { onShow, onHide } from '@dcloudio/uni-app'
  import { patientDelApi, patientMylistApi } from '@/services/archive.js'

  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
      },
    },
  ])

  const list = ref([])
  const getList = async () => {
    const res = await patientMylistApi()
    list.value = res.data
  }

  onShow(() => getList())

  const onClick = async (e) => {
    const res = await uni.showModal({
      title: `你确定要删除"${e.name}"吗？`,
      confirmColor: 'red',
    })
    if (res.confirm) {
      await patientDelApi(e.id)
      await getList()
    }
  }
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <!-- 
          uniapp：以后在循环遍历时，如果有数据变更，一定要保证key也要跟着变，不变的话，小程序可能渲染有问题
                  可能会复用之前的节点，导致渲染无法更新
         -->
        <uni-swipe-action-item
          v-for="v of list"
          :key="v.id + '_' + v.defaultFlag"
          :right-options="swipeOptions"
          @click="onClick(v)"
        >
          <view class="archive-card" :class="{ active: v.defaultFlag === 1 }">
            <view class="archive-info">
              <text class="name">{{ v.name }}</text>
              <text class="id-card">{{
                v.idCard.replace(/^(.{3})(.+)(.{4})$/, '$1******$3')
              }}</text>
              <text v-if="v.defaultFlag === 1" class="default">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ v.genderValue }}</text>
              <text class="age">{{ v.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="'/subpkg_archive/form/index?id=' + v.id"
            >
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
              />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="list.length < 6" class="archive-card">
        <navigator
          class="add-link"
          hover-class="none"
          url="/subpkg_archive/form/index"
        >
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './index.scss';
</style>
